<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>碎片化轮播</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #f0f0f0;
        }

        .fragment-carousel {
            width: 800px;
            height: 500px;
            position: relative;
            overflow: hidden;
        }

        .fragment-container {
            width: 100%;
            height: 100%;
            position: relative;
        }

        .slide {
            position: absolute;
            width: 100%;
            height: 100%;
            opacity: 0;
            transition: opacity 0.5s ease;
        }

        .slide.active {
            opacity: 1;
        }

        .fragments-wrapper {
            position: absolute;
            width: 100%;
            height: 100%;
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            grid-template-rows: repeat(4, 1fr);
            gap: 2px;
            opacity: 0;
            pointer-events: none;
        }

        .fragment {
            background-size: 800% 800%;
            transform: scale(1);
            opacity: 1;
            transition: all 0.5s ease;
        }

        .fragments-wrapper.animating {
            opacity: 1;
        }

        .fragments-wrapper.animating .fragment {
            transform: scale(0);
            opacity: 0;
        }

        .controls {
            position: fixed;
            bottom: 20px;
            display: flex;
            gap: 10px;
            z-index: 100;
        }

        .control-btn {
            padding: 10px 20px;
            background: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .control-btn:hover {
            background: #555;
        }
    </style>
</head>
<body>
    <div class="fragment-carousel">
        <div class="fragment-container">
            <div class="slide active"><img src="../img/1 (1).png" alt="Image 1" style="width:100%;height:100%;object-fit:cover;"></div>
            <div class="slide"><img src="../img/1 (2).png" alt="Image 2" style="width:100%;height:100%;object-fit:cover;"></div>
            <div class="slide"><img src="../img/1 (3).png" alt="Image 3" style="width:100%;height:100%;object-fit:cover;"></div>
            <div class="slide"><img src="../img/1 (4).png" alt="Image 4" style="width:100%;height:100%;object-fit:cover;"></div>
            <div class="slide"><img src="../img/1 (5).png" alt="Image 5" style="width:100%;height:100%;object-fit:cover;"></div>
        </div>
    </div>
    <div class="controls">
        <button class="control-btn prev">上一张</button>
        <button class="control-btn next">下一张</button>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const container = document.querySelector('.fragment-container');
            const slides = document.querySelectorAll('.slide');
            const prevBtn = document.querySelector('.prev');
            const nextBtn = document.querySelector('.next');
            
            let currentIndex = 0;
            const totalSlides = slides.length;
            let isAnimating = false;

            function createFragments(img) {
                const wrapper = document.createElement('div');
                wrapper.className = 'fragments-wrapper';
                
                const imgUrl = img.src;
                const fragmentSize = 100 / 4; // 4x4 grid

                for (let row = 0; row < 4; row++) {
                    for (let col = 0; col < 4; col++) {
                        const fragment = document.createElement('div');
                        fragment.className = 'fragment';
                        fragment.style.backgroundImage = `url(${imgUrl})`;
                        fragment.style.backgroundPosition = `${-col * fragmentSize}% ${-row * fragmentSize}%`;
                        fragment.style.backgroundSize = '400% 400%';
                        fragment.style.transitionDelay = `${Math.random() * 0.3}s`;
                        wrapper.appendChild(fragment);
                    }
                }

                return wrapper;
            }

            function updateCarousel(direction) {
                if (isAnimating) return;
                isAnimating = true;

                const currentSlide = slides[currentIndex];
                const currentImg = currentSlide.querySelector('img');
                const fragments = createFragments(currentImg);
                currentSlide.appendChild(fragments);

                // Start fragment animation
                setTimeout(() => {
                    fragments.classList.add('animating');
                }, 50);

                // Update current index
                currentSlide.classList.remove('active');
                if (direction === 'next') {
                    currentIndex = (currentIndex + 1) % totalSlides;
                } else {
                    currentIndex = (currentIndex - 1 + totalSlides) % totalSlides;
                }

                // Show next slide
                slides[currentIndex].classList.add('active');

                // Clean up fragments
                setTimeout(() => {
                    currentSlide.removeChild(fragments);
                    isAnimating = false;
                }, 1000);
            }

            prevBtn.addEventListener('click', () => {
                if (!isAnimating) updateCarousel('prev');
            });

            nextBtn.addEventListener('click', () => {
                if (!isAnimating) updateCarousel('next');
            });

            // Auto play
            setInterval(() => {
                if (!isAnimating) updateCarousel('next');
            }, 4000);
        });
    </script>
</body>
</html>